#coding=utf8
from django.db import models
from django.contrib.auth.models import User
from docshare import settings
import datetime

class profile(models.Model):
    user = models.OneToOneField(User)
    real_name = models.CharField(max_length=50)
    department = models.CharField(max_length=50,blank=True)
    phone = models.CharField(max_length=50,blank=True)

class catalog(models.Model):
    name=models.CharField(max_length=255)
    pic=models.ImageField(upload_to='upSys/%Y%m')
    parentId=models.ForeignKey('catalog',blank=True,default=0)
    date = models.DateTimeField(unique_for_date="pub_date",auto_now=True, auto_now_add=True)
    def __unicode__(self):
        return self.name
    def get_leveltwo_url(self):
        return "/%s/resource/show_leveltwo_resource/%d"%(settings.SITE_ROOT,self.id)
    def get_files_url(self):
        return "/%s/resource/show_files/%d"%(settings.SITE_ROOT,self.id)
    def get_img_url(self):
        return settings.DEFAULT_UPLOAD_URL+str(self.pic)

class upFile(models.Model):
    name=models.CharField(max_length=255)
    about=models.CharField(max_length=255)
    datetime=models.DateTimeField(unique_for_date="pub_date",auto_now=True, auto_now_add=True)
    filepath=models.FileField(upload_to='upFile/%Y%m')
    filepic=models.ImageField(upload_to='upImage/%Y%m',blank=True,default=None)
    size=models.IntegerField()
#   type=models.ForeignKey('fileType')
    prefix_one = models.CharField(max_length=255)
    prefix_two = models.CharField(max_length=255)
    prefix_three = models.CharField(max_length=255)
    is_del = models.BooleanField(blank=True,default=False)
    del_user = models.CharField(max_length=50,blank=True)
    cata=models.ForeignKey('catalog')
    user=models.ForeignKey(User)
    class Meta:
        ordering = ['-datetime']

    def __unicode__(self):
        return self.name
    def getThumb(self):
        if self.filepic:
            return settings.DEFAULT_UPLOAD_URL+str(self.filepic)[:str(self.filepic).find('.')]+"_thumb.jpg"
        else:
            return "/site_media/images/no_pic.jpg"
    def getUser(self):
        return self.user.username
    def getCata(self):
        return self.cata.name
    def getImgPath(self):
        if self.filepic:
            return settings.DEFAULT_UPLOAD_URL+str(self.filepic)
        else:
            return "/site_media/images/no_pic.jpg"
    def getFilePath(self):
        return settings.DEFAULT_UPLOAD_URL+str(self.filepath)
    def getFile(self):
        return "/%s/resource/show_context/%d"%(settings.SITE_ROOT,self.id)
    def getDatetime(self):
        format="%Y-%m-%d %H:%M:%S"
        return time.strptime(self.datetime,format)
    def isNew(self):
        now_date = datetime.datetime.now()- self.datetime
        if now_date.days<3:
            return True
        else:
            return False


class pub(models.Model):
    name = models.CharField(max_length=50)
    info = models.CharField(max_length=255)
    date = models.DateTimeField(unique_for_date="pub_date",auto_now=True,auto_now_add=True)
    user = models.ForeignKey(User)
    class Meta:
        ordering = ['-date']
    def __unicode__(self):
        return self.name
    def get_list_link(self):
        return "/%s/affiche/list/"%(settings.SITE_ROOT)
    def get_id_link(self):
        return "/%s/affiche/show/%d"%(settings.SITE_ROOT,self.id)
    def get_del_link(self):
        return "/%s/affiche/del/%d"%(settings.SITE_ROOT,self.id)
    def get_edit_link(self):
        return "/%s/affiche/edit/%d"%(settings.SITE_ROOT,self.id)

class dictionaries(models.Model):
    name = models.CharField(max_length=50)
    type = models.CharField(max_length=50)
    memo = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class subject(models.Model):
    title = models.CharField(max_length=50)
    context = models.TextField()
    date = models.DateTimeField(unique_for_date="pub_date",auto_now=True,auto_now_add=True)
    author = models.ForeignKey(User)
    read_nums = models.IntegerField(default=0)
    repe_nums = models.IntegerField(default=0)
    def __unicode__(self):
        return self.title
    def get_repy_path(self):
        return '/%s/forum/show_replies/%d'%(settings.SITE_ROOT,self.id)

class reply(models.Model):
    context = models.TextField()
    date = models.DateTimeField(unique_for_date="pub_date",auto_now=True,auto_now_add=True)
    subject = models.ForeignKey('subject')
    author = models.ForeignKey(User)
    class Meta:
        ordering = ['-date']
    def __unicode__(self):
        return self.title
